Skip to content

Postfix - Installation und Einrichtung

Postfix

Installation der Debian Pakete

Als erstes installieren wir die benötigten Pakete.

apt install postfix postfix-mysql dovecot-imapd dovecot-lmtpd dovecot-pop3d dovecot-mysql redis-tools

MySQL Konfiguration für das postfixadmin Datenbank Schema

Nun erstellen wir ein Verzeichnis /etc/postfix/sql wo wir die SQL Querys für Postfix speichern.

mkdir -p /etc/postfix/sql

Wir setzen uns das Datenbank Passwort als Variable $sqlpw und verwenden diese in der Konfiguration.

sqlpw='<MYSECRET>'

Nun erstellen wir uns mysql_virtual_domains_maps.cf. Das Passwort haben wir zu Begin weiter oben in $sqlpw definiert.

cat <<CONF | tee /etc/postfix/sql/mysql_virtual_domains_maps.cf >/dev/null
user = postfixadmin
password = $sqlpw
hosts = 127.0.0.1
dbname = postfixadmin
query = SELECT domain FROM domain WHERE domain='%s' AND active = '1'
CONF

Als nächstes erstellen wir uns mysql_virtual_alias_maps.cf. Das Passwort haben wir zu Begin weiter oben in $sqlpw definiert.

cat <<CONF | tee /etc/postfix/sql/mysql_virtual_alias_maps.cf >/dev/null
user = postfixadmin
password = $sqlpw
hosts = 127.0.0.1
dbname = postfixadmin
query = SELECT goto FROM alias WHERE address='%s' AND active = '1'
CONF

Als nächstes erstellen wir uns mysql_virtual_alias_domain_catchall_maps.cf. Das Passwort haben wir zu Begin weiter oben in $sqlpw definiert.

cat <<CONF | tee /etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf >/dev/null
user = postfixadmin
password = $sqlpw
hosts = 127.0.0.1
dbname = postfixadmin
query  = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'
CONF

Als nächstes erstellen wir uns mysql_virtual_alias_domain_maps.cf. Das Passwort haben wir zu Begin weiter oben in $sqlpw definiert.

cat <<CONF | tee /etc/postfix/sql/mysql_virtual_alias_domain_maps.cf >/dev/null
user = postfixadmin
password = $sqlpw
hosts = 127.0.0.1
dbname = postfixadmin
query = SELECT goto FROM alias,alias_domain WHERE alias_domain.alias_domain = '%d' and alias.address = CONCAT('%u', '@', alias_domain.target_domain) AND alias.active = 1 AND alias_domain.active='1'
CONF

Als nächstes erstellen wir uns mysql_virtual_mailbox_maps.cf. Das Passwort haben wir zu Begin weiter oben in $sqlpw definiert.

cat <<CONF | tee /etc/postfix/sql/mysql_virtual_mailbox_maps.cf >/dev/null
user = postfixadmin
password = $sqlpw
hosts = 127.0.0.1
dbname = postfixadmin
query = SELECT maildir FROM mailbox WHERE username='%s' AND active = '1'
CONF

Als nächstes erstellen wir uns mysql_virtual_alias_domain_mailbox_maps.cf. Das Passwort haben wir zu Begin weiter oben in $sqlpw definiert.

cat <<CONF | tee /etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf >/dev/null
user = postfixadmin
password = $sqlpw
hosts = 127.0.0.1
dbname = postfixadmin
query = SELECT maildir FROM mailbox,alias_domain WHERE alias_domain.alias_domain = '%d' and mailbox.username = CONCAT('%u', '@', alias_domain.target_domain) AND mailbox.active = 1 AND alias_domain.active='1'
CONF

Sobald die MySQL-Konfigurationsdateien erstellt sind, aktualisieren Sie die Konfiguration von Postfix.

postconf -e "virtual_mailbox_domains = mysql:/etc/postfix/sql/mysql_virtual_domains_maps.cf"
postconf -e "virtual_alias_maps = mysql:/etc/postfix/sql/mysql_virtual_alias_maps.cf, mysql:/etc/postfix/sql/mysql_virtual_alias_domain_maps.cf, mysql:/etc/postfix/sql/mysql_virtual_alias_domain_catchall_maps.cf"
postconf -e "virtual_mailbox_maps = mysql:/etc/postfix/sql/mysql_virtual_mailbox_maps.cf, mysql:/etc/postfix/sql/mysql_virtual_alias_domain_mailbox_maps.cf"

Hinweis

Mit dem postconfBefehl können Sie die tatsächliche Konfiguration anzeigen, Konfigurationswerte ändern oder andere Konfigurationsinformationen zum Postfix-Mailsystem anzeigen.

Die Lieferung vor Ort erfolgt durch Dovecot. Es nimmt E-Mails von einem MTA (Postfix) entgegen und stellt sie an das Postfach eines lokalen Benutzers zu.

Postfix Transport-Konfiguration

postconf -e "virtual_transport = lmtp:unix:private/dovecot-lmtp"
postconf -e "default_transport = smtp"
postconf -e "relay_transport = smtp"

Erläuterung

Die eigentliche Zustellung der virtuellen Postfächer erfolgt per LMTP an Dovecot. Dovecot speichert die Mail im Maildir

Postfix TLS/SSL Konfiguration

Die TLS-Parameter konfigurieren wir mithilfe des Let's encrypt SSL-Zertifikats. Der Pfad zu den Zertifikaten muss ggf. angepasst werden. In meinem Beispile setzen wir die $domain auf exaqmple.com.

domain=example.com
postconf -e 'smtpd_tls_security_level = may'
postconf -e 'smtp_tls_security_level = may'

postconf -e "smtpd_tls_auth_only = yes"
postconf -e 'smtp_tls_note_starttls_offer = yes'
postconf -e 'smtpd_tls_loglevel = 1'
postconf -e 'smtpd_tls_received_header = yes'

postconf -e "smtpd_tls_cert_file = /etc/apache2/custom.d/certs.d/${domain}/fullchain.pem"
postconf -e "smtpd_tls_key_file = /etc/apache2/custom.d/certs.d/${domain}/privkey.pem"

postconf -e "smtpd_tls_protocols = !SSLv2,!SSLv3,!TLSv1,!TLSv1.1"
postconf -e "smtp_tls_protocols = !SSLv2,!SSLv3,!TLSv1,!TLSv1.1"
postconf -e "smtpd_tls_mandatory_protocols = !SSLv2,!SSLv3,!TLSv1,!TLSv1.1"
postconf -e "smtp_tls_mandatory_protocols = !SSLv2,!SSLv3,!TLSv1,!TLSv1.1"

Erläuterung

  • Zertifikat und Private Key werden eingebunden.
  • may erlaubt TLS, erzwingt es aber nicht.
  • Unsichere Protokolle (SSLv2/v3, TLS 1.0/1.1) werden deaktiviert.
  • smtpd_tls_auth_only = yes erlaubt SMTP-Authentifizierung nur über TLS.

Basis Konfiguration des Mailservers

Die Basis Identität unseres Mailservers definieren wir wie folgt.

postconf -e "myhostname = box01.${domain}"
postconf -e "myorigin = /etc/mailname"
postconf -e "mydestination = \$myhostname, box01, localhost.localdomain, localhost"
postconf -e "inet_interfaces = all"
postconf -e "inet_protocols = ipv4"

Erläuterung

  • myhostname definiert den vollständigen Hostnamen des Mailservers.
  • myorigin bestimmt die Domain, mit der lokal versendete Mails signiert werden.
  • mydestination legt fest, für welche Domains der Server selbst zuständig ist (lokale Zustellung).
  • inet_interfaces = all sorgt dafür, dass Postfix auf allen Interfaces lauscht.
  • inet_protocols = ipv4 beschränkt den Betrieb auf IPv4.

Postfix Netzwerk- und Relay-Konfiguration

Die Netzwerk und Relay Einstellungen des Mailservers definieren wir wie folgt.

postconf -e "mynetworks_style = host"
postconf -e "mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128"
postconf -e "smtpd_relay_restrictions = permit_mynetworks permit_sasl_authenticated defer_unauth_destination"
postconf -e "smtpd_recipient_restrictions = permit_sasl_authenticated,permit_mynetworks,reject_unauth_destination"

Erläuterung

  • mynetworks definiert vertrauenswürdige Clients.
  • Authentifizierte Benutzer dürfen relayen.
  • Nicht autorisierte Zustellungen werden verhindert → Open Relay Schutz.

Postfix SASL-Authentifizierung über Dovecot

Die SASL-Authentifizierung über Dovecot definieren wir wie folgt.

postconf -e "smtpd_sasl_type = dovecot"
postconf -e "smtpd_sasl_path = private/auth"
postconf -e "smtpd_sasl_security_options = noanonymous"
postconf -e "broken_sasl_auth_clients = yes"

Erläuterung

  • Postfix nutzt Dovecot zur Authentifizierung.
  • Clients authentifizieren sich über den UNIX-Socket private/auth.
  • Anonyme Logins sind nicht erlaubt.

Postfix Mailgröße und Adressierung

Die Mailgröße und Zustellung des Mailservers definieren wir wie folgt.

postconf -e "message_size_limit = 52428800"
postconf -e "mailbox_size_limit = 0"
postconf -e "recipient_delimiter = +"

Erläuterung

  • Maximale Mailgröße: 50 MB
  • Kein Limit für Mailboxgröße (Quota übernimmt Dovecot)
  • user+tag@domain wird unterstützt

Postfix Postscreen-Konfiguration (Spam-Schutz)

Postscreen (Spam-Schutz vor SMTP) definieren wir für den Mailserver wie folgt.

postconf -e "postscreen_greet_action = enforce"
postconf -e "postscreen_dnsbl_sites = zen.spamhaus.org*2 bl.spamcop.net"
postconf -e "postscreen_dnsbl_action = enforce"
postconf -e "postscreen_access_list = permit_mynetworks, cidr:/etc/postfix/postscreen_access.cidr"

Erläuterung

  • DNSBL-Abfragen gegen Spamhaus und Spamcop
  • Ablehnung bekannter Spam-IPs bereits vor SMTP-Dialog
  • Vertrauenswürdige Netze werden ausgenommen

Services in der master.cf einrichten

Aktivierung von postscreen (Port 25 Hardening)

Standardmäßig lauscht smtpd direkt auf Port 25. In dieser Konfiguration wird stattdessen postscreen vorgeschaltet:

smtp      inet  n       -       y       -       1       postscreen
smtpd     pass  -       -       y       -       -       smtpd
dnsblog   unix  -       -       y       -       0       dnsblog
tlsproxy  unix  -       -       y       -       0       tlsproxy

Zweck

  • Schutz vor SMTP-Bots
  • Reduktion von Spam-Verbindungen
  • DNSBL-Prüfung vor Übergabe an smtpd

Technischer Ablauf

Internet → Port 25 → postscreen → (DNSBL / Checks) → smtpd

Damit wird die eigentliche SMTP-Instanz entlastet und unnötige Verbindungen werden frühzeitig abgewiesen.

Aktivierung von Submission (Port 587)

Der Submission-Port ist für authentifizierte Mail-Clients vorgesehen.

submission inet n       -       y       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING

Besonderheiten

  • TLS wird erzwungen (encrypt)
  • SMTP-Authentifizierung ist aktiviert
  • Nur authentifizierte Clients dürfen senden
  • Milter erhalten das Makro ORIGINATING

Wirkung

Clients können ausschließlich verschlüsselt und authentifiziert E-Mails versenden. Offenes Relaying wird dadurch verhindert.

Aktivierung von SMTPS (Port 465)

Zusätzlich wurde SMTPS aktiviert:

smtps     inet  n       -       y       -       -       smtpd
  -o syslog_name=postfix/smtps
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING

Besonderheiten

  • TLS im Wrapper-Modus (TLS vor SMTP-Handshake)
  • Authentifizierung erforderlich
  • Nur authentifizierte Clients erlaubt

Zweck

Kompatibilität mit älteren oder speziellen Mail-Clients, die SMTPS bevorzugen.

Aktivierung von Maildrop (Pipe Service)

maildrop  unix  -       n       n       -       -       pipe
  flags=DRXhu user=vmail argv=/usr/bin/maildrop -d ${recipient}

Zweck

  • Zustellung über Maildrop
  • Ausführung unter dem Benutzer vmail
  • Ermöglicht serverseitige Filterung (z. B. Sieve oder individuelle Regeln)

Diese Konfiguration wird verwendet, wenn virtuelle Mailboxen oder externe Zustellmechanismen im Einsatz sind.

Neustart und testen der Konfiguration

Nachdem wir dien Postfix Mailserver soweit konfigiriert haben, prüfen wir die Konfiguration und starten den Postfix neu.

postfix check && systemctl restart postfix

Die aktuell gesetzen Parameter können wir uns mit folgendem Befehl anzeigen lassen.

postconf -n   # zeigt aktive config
postconf -M   # zeigt aktive master‑Dienste